import { _decorator, Button, Label, Node, Sprite, SpriteAtlas } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { Respones, RewardType } from '../../Config/GameCfg';
import { ActivityProps, GetHD_LoginRewardGetRes, GetHD_LoginRewardRes } from '../../../AssetPackage/Props/Hall/GetHDLoginReward';
const { ccclass, property } = _decorator;

/**
 * 登录奖励
 */
@ccclass('GameHD3_Ctr')
export class GameHD3_Ctr extends UIComponent {
    btnNode: Node;
    mapNode: Node;
    isClick: boolean = true;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(1);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.AddButtonListener('mask',this,this.OnBtnCloseClick1);
        this.AddButtonListener('root/btn',this,this.onBtnLinQu);
        this.btnNode = this.ViewNode('root/btn');
        this.mapNode = this.ViewNode('root/map');
    }

    private InitData(){
        let children = this.mapNode.children;
        children.forEach(item => {
            item.active = false;
        });

        globalThis.gameMgr._GameHallMsg.GetHD_LoginRewardReq();
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgonGetHD_LoginRewardRes,this.onGetHD_LoginRewardRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGetHD_LoginRewardGetRes,this.onGetHD_LoginRewardGetRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgonGetHD_LoginRewardRes,this.onGetHD_LoginRewardRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGetHD_LoginRewardGetRes,this.onGetHD_LoginRewardGetRes,this);
    }

    public OnBtnCloseClick1(): void{
        SceneMgr.inst.showHall();
    }

    private setBtnStatus(isGet: boolean){
        let btn = this.btnNode.getComponent(Button);
        let sp = this.btnNode.getComponent(Sprite);

        if(isGet){
            sp.grayscale = true;
            btn.interactable = false;
        }else{
            sp.grayscale = false;
            btn.interactable = true;
        }
    }

    //领取
    private onBtnLinQu(btn: Button){
        if(!this.isClick){
            return;
        }
        this.isClick = false;
        globalThis.gameMgr._GameHallMsg.GetHD_LoginRewardGetReq();
    }

    private setItemData(node: Node,data:ActivityProps){
        let atals = ResManager.inst.getAsset(AbNames.GameHall,'Atals/GameHall', SpriteAtlas) as SpriteAtlas;
        let spbg1 = node.getComponent(Sprite);
        let spbg = node.getChildByPath('sp1').getComponent(Sprite);
        let spIcon = node.getChildByPath('NewPlayer_icon').getComponent(Sprite);
        let labDay = node.getChildByPath('labDay').getComponent(Label);
        let labNum = node.getChildByPath('labNum').getComponent(Label);
        let nDain = node.getChildByPath('spDian');
        let nMask = node.getChildByPath('spMask');
        let nGou = node.getChildByPath('spGou');
        nDain.active = false;
        nMask.active = false;
        nGou.active = false;

        if(data.idx){
            labDay.string =  `第${data.idx}天`;
        }

        let sfIcon = null;
        if(data.rewardType === RewardType.TYpe_QiuNum){
            sfIcon = atals.getSpriteFrame('NewPlayer_icon');
        }else if(data.rewardType === RewardType.Type_Quan){
            sfIcon = atals.getSpriteFrame('NewPlayer_icon3');
        }
        if(sfIcon){
            spIcon.spriteFrame = sfIcon;
        }
        if(data.rewardNum){
            labNum.string = data.rewardNum.toString();
        }

        // 0:未领取,1:已领取,2:今日待领取(红点),3:待领取
        let isGet = data.whetherToReceive || 0;
        if(isGet == 1){ //已经领取
            let sfBg1 = atals.getSpriteFrame('HD3_3');
            if(sfBg1){
                spbg1.spriteFrame = sfBg1;
            }
            let sfBg12 = atals.getSpriteFrame('HD3_4');
            if(sfBg12){
                spbg.spriteFrame = sfBg12;
            }
            nMask.active = true;
            nGou.active = true;
        }else if(isGet == 0){ //未领取
            let sfBg1 = atals.getSpriteFrame('HD3_1');
            if(sfBg1){
                spbg1.spriteFrame = sfBg1;
            }
            let sfBg12 = atals.getSpriteFrame('HD3_5');
            if(sfBg12){
                spbg.spriteFrame = sfBg12;
            }


            // let isTaday = data.isToday||0;
            // if(isTaday === 0){ //是当天
            //     nDain.active = true;
            // }else if(isTaday === 1){ //不是当天
            //     nDain.active = false;
            // }
        }else if(isGet === 2){ //:今日待领取(红点)

        }else if(isGet === 3){//3:待领取

        }
    }

    //每周登录奖励--配置
    private onGetHD_LoginRewardRes(data: GetHD_LoginRewardRes) {
        console.log('=======onGetHD_LoginRewardRes=============', data);
        if (this.hasProInObj(data, 'activityProps')) {
            let list = data.activityProps;
            let children = this.mapNode.children;
            for (let i = 0; i < list.length; i++) {
                const cfg: ActivityProps = list[i];
                this.setItemData(children[i], cfg);
                children[i].active = true;
            }
        }
    }
   
    //每周登录奖励--领取
    private onGetHD_LoginRewardGetRes(data: GetHD_LoginRewardGetRes){
        console.log('=========onGetHD_LoginRewardGetRes =========');
        if(this.hasProInObj(data,'code')){
            if(data.code === Respones.OK){
                if (this.hasProInObj(data, 'activityProps')) {
                    let list = data.activityProps;
                    let children = this.mapNode.children;
                    for (let i = 0; i < list.length; i++) {
                        const cfg: ActivityProps = list[i];
                        this.setItemData(children[i], cfg);
                        children[i].active = true;
                    }
                }
            }else{
                console.log('=========onGetHD_LoginRewardGetRes err=========',data.code);
            }
        }
    }
}


